<?php
/**
 * 75汽车设计
 */
require_once('util/txt2array.php');
require_once('util/array2csv.php');
header("content-type:text/html;charset=utf-8");

$name = "src75";
$lines = txt2Array("question/{$name}.txt");

$lines = array_values(array_filter($lines, "trim"));

foreach ($lines as &$val) {
    $val = trim($val);
}

$lines = str_replace("\t", "", $lines);


$data = [];
$type = ""; //题型
$stem = ""; //题干
$operation = ""; //选项
$answer = "";   //答案
$score = "2";
$level = "3";
$courseCode = '="00067001"';


$dataMap = null;
//处理dataMap
foreach ($lines as $item) {
//    拼装题型$type
    if ($item == '一、单项选择题') {
        $score = 1;
        $type = "单选题";
        continue;
    }
    if ($item == '二、多项选择题') {
        $score = 2;
        $type = "多选题";
        continue;
    }

//    拼装难度$level
    if (strpos($item, "%=") !== false) {
        $levelNum = ["中等偏易题目" => 2, "中等偏难题目" => 4, "较容易题目" => 1, "较难题目" => 5,];
        foreach ($levelNum as $k => $v) {
            if (strpos($item, $k) !== false) {
                $level = $v;
                break;
            }
        }
        continue;
    }


//拼装题干$stem
    if (preg_match("/^[0-9]{1,3}[.．、]?/", $item)) {
        $stem = $item;
        $stem = str_replace("(★★★★★)", "", $stem);
        $stem = str_replace("(★★★)", "", $stem);
        $stem = str_replace("(★)", "", $stem);

        //        拼装答案$answer
        $stem = str_replace(" ", "", $stem);
        $stem = str_replace("　", "", $stem);
        preg_match("/(\([A-Z]{1,5}\))/", $stem, $matches);
        $stem = preg_replace("/(\([A-Z]{1,5}\))/", "（  ）", $stem);
        if (empty($matches)) {
            preg_match("/（[A-Z]{1,5}）/", $stem, $matches);
            $stem = preg_replace("/（[A-Z]{1,5}）/", "（  ）", $stem);
        }
        if (empty($matches[0])) {
            echo $stem . "\n";
        } else {
            $answer = $matches[0];
            $answer = str_replace("（", "", $answer);
            $answer = str_replace("）", "", $answer);
            $answer = str_replace("(", "", $answer);
            $answer = str_replace(")", "", $answer);
            $answer = implode(";", str_split($answer));
        }
        continue;
    }
//    拼装选择
    if (strpos($item, "A") === 0
        || strpos($item, "B") === 0
        || strpos($item, "C") === 0
        || strpos($item, "D") === 0
        || strpos($item, "E") === 0
        || strpos($item, "F") === 0
        || strpos($item, "G") === 0
        || strpos($item, "Ａ") === 0
        || strpos($item, "Ｂ") === 0
        || strpos($item, "Ｃ") === 0
        || strpos($item, "Ｄ") === 0
        || strpos($item, "（A）") === 0
        || strpos($item, "（B）") === 0
        || strpos($item, "（C）") === 0
        || strpos($item, "（D）") === 0
    ) {
        $operation .= $item;
    }


//答案
    if (strpos($item, "答案：") === 0 || strpos($item, "正确答案") === 0 || strpos($item, "答案:") === 0 || strpos($item, "答案") === 0) {
        $anstmp = explode("答案：", $item);
        if (empty($anstmp[1])) {
            $anstmp = explode("答案:", $item);
        }
        if (empty($anstmp[1])) {
            $anstmp = explode("答案", $item);
        }
        //        拼装答案$answer
        preg_match("/(^[A-Z]{1,5})/", trim($anstmp[1]), $matches);
        if (empty($matches[0])) {
            echo $stem . "\n";
        }
        $answer = $matches[0];
        $answer = implode(";", str_split($answer));

    }

//组装整体内容
    if (!empty($stem) && !empty($operation) && !(empty($dataMap[$stem]) && empty($answer))) {
        $operation = Operation::splitOptions($operation);
        $stem = trim($stem);

        if (empty($dataMap[$stem])) {
            $dataMap[$stem] = [
                "type" => $type,
                "operation" => $operation,
                "level" => $level,
                "score" => $score,
                "answer" => $answer,
            ];
            $answer = "";
        } else {
            $dataMap[$stem]["operation"] .= $operation;
        }
        $operation = "";
    }
}

//处理data
foreach ($dataMap as $kStem => $value) {
    $kStem = preg_replace("/^[0-9]{1,3}(．)?(、)?[.．、]?/", "", $kStem);
    $data[] = [
        $value["type"],
        "",
        $kStem,
        $value['operation'],
        $value["answer"],
        "",
        $value['level'],
        $value['score'],
        $courseCode];
}


$file_name = "util\csv\{$name}";
toCSV($file_name, $data, getHeaders());